package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MenuServiceImpl
 * @Description
 * @Author Jingxiao.Ye
 * @Date 2020/1/14 10:26
 * @Version V1.0
 * @Since JDK 1.8
 */
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private RoleDao roleDao;

    @Override
    
    /** 
    * @Description: 根据条件查询menu的分页信息,忽略空格
    * @Param: [queryPageBean] 
    * @return: com.itheima.entity.PageResult 
    * @Author: Jingxiao.Ye
    * @Date: 2020/1/16 
    */ 
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //过滤空格
        if(queryString!=null){
            queryString = queryString.replaceAll(" ", "");
        }
        //queryString = queryString.replace(" ", "");
        PageHelper.startPage(currentPage,pageSize);//分页插件，会在执行sql之前将分页关键字追加到SQL后面
        Page<Map> page = menuDao.findAll(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override

    /**
    * @Description: 根据菜单ID查询角色
    * @Param: [id]
    * @return: java.util.Map
    * @Author: Jingxiao.Ye
    * @Date: 2020/1/16
    */
    public Map findRoleByMenuId(Integer id) {
        List<Map> tableData = roleDao.findMapAll();
        List<Map> multipleSelection = menuDao.findRoleByMenuId(id);
        Map map = new HashMap<>();
        map.put("tableData",tableData);
        map.put("multipleSelection",multipleSelection);
        return map;
    }

    @Override

    /**
    * @Description: 修改菜单拥有的角色
    * @Param: [menuId, roleIds]
    * @return: void
    * @Author: Jingxiao.Ye
    * @Date: 2020/1/16
    */
    public void modifyMenuRole(Integer menuId,List<Integer> roleIds){
        menuDao.delMenuRole(menuId);
        for (Integer roleId : roleIds) {
            menuDao.addMenuRole(menuId,roleId);
        }
        //删除如果是一级目录下面的二级目录中
        menuDao.delMenuChildren(menuId,roleIds);
        //增加如果是二级目录,则同时增加1级目录
        Integer parentMenuByMenuId = menuDao.findParentMenuByMenuId(menuId);
        if(parentMenuByMenuId!=null){
            menuDao.delMenuRole(parentMenuByMenuId);
            for (Integer roleId : roleIds) {
                menuDao.addMenuRole(parentMenuByMenuId,roleId);
            }
        }
    }

    @Override

    /**
    * @Description: 通过用户名查询所有menu
    * @Param: [username]
    * @return: java.util.List<com.itheima.pojo.Menu>
    * @Author: Jingxiao.Ye
    * @Date: 2020/1/16
    */
    public List<Menu> findMenuByUsername(String username){
        List<Menu> menus = menuDao.findMenuByUsername(username);
        return menus;
    }
}
